/***************************************************************
 * Name:      ProjectMain.cpp
 * Purpose:   Code for Application Frame
 * Author:     ()
 * Created:   2012-01-12
 * Copyright:  ()
 * License:
 **************************************************************/

#include "../include/ProjectMain.h"
#include <wx/msgdlg.h>
#include <wx/tglbtn.h>

//(*InternalHeaders(ProjectFrame)
#include <wx/settings.h>
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(ProjectFrame)
const long ProjectFrame::ID_ADD_BONE_BUTTON = wxNewId();
const long ProjectFrame::ID_TIMELINE = wxNewId();
const long ProjectFrame::ID_BONE_TREE = wxNewId();
const long ProjectFrame::ID_STATICTEXT1 = wxNewId();
const long ProjectFrame::ID_TEXTCTRL2 = wxNewId();
const long ProjectFrame::ID_STATICTEXT6 = wxNewId();
const long ProjectFrame::ID_MIN_SPIN = wxNewId();
const long ProjectFrame::ID_STATICTEXT7 = wxNewId();
const long ProjectFrame::ID_MAX_SPIN = wxNewId();
const long ProjectFrame::ID_STATICTEXT8 = wxNewId();
const long ProjectFrame::ID_CUSTOM1 = wxNewId();
const long ProjectFrame::ID_STATICTEXT9 = wxNewId();
const long ProjectFrame::ID_CHECKBOX1 = wxNewId();
const long ProjectFrame::ID_PANEL1 = wxNewId();
const long ProjectFrame::ID_STATICTEXT2 = wxNewId();
const long ProjectFrame::ID_TEXTCTRL1 = wxNewId();
const long ProjectFrame::ID_BUTTON1 = wxNewId();
const long ProjectFrame::ID_STATICTEXT3 = wxNewId();
const long ProjectFrame::ID_TEXTCTRL3 = wxNewId();
const long ProjectFrame::ID_STATICTEXT4 = wxNewId();
const long ProjectFrame::ID_TEXTCTRL4 = wxNewId();
const long ProjectFrame::ID_PANEL2 = wxNewId();
const long ProjectFrame::ID_TABS = wxNewId();
const long ProjectFrame::ID_CUSTOM3 = wxNewId();
const long ProjectFrame::ID_CUSTOM2 = wxNewId();
const long ProjectFrame::ID_CUSTOM4 = wxNewId();
const long ProjectFrame::idMenuNewFile = wxNewId();
const long ProjectFrame::idMenuSaveAs = wxNewId();
const long ProjectFrame::idMenuOpen = wxNewId();
const long ProjectFrame::idMenuImport = wxNewId();
const long ProjectFrame::idMenuQuit = wxNewId();
const long ProjectFrame::idMenuAbout = wxNewId();
const long ProjectFrame::ID_STATUSBAR1 = wxNewId();
//*)

const long ProjectFrame::ID_CANVAS = wxNewId();

BEGIN_EVENT_TABLE(ProjectFrame,wxFrame)
    //(*EventTable(ProjectFrame)
    //*)
    EVT_UPDATE_UI(ID_CANVAS,ProjectFrame::CanvasUpdate)
    EVT_SPINCTRL(ID_MIN_SPIN, ProjectFrame::updateAngleMin)
    EVT_SPINCTRL(ID_MAX_SPIN, ProjectFrame::updateAngleMax)
    EVT_SPINCTRL(ID_CUSTOM1, ProjectFrame::updateLoops)
    EVT_TEXT_ENTER(ID_TEXTCTRL2, ProjectFrame::updateBoneName)
    EVT_TEXT_ENTER(ID_TEXTCTRL3, ProjectFrame::updateSpriteXY)
    EVT_TEXT_ENTER(ID_TEXTCTRL4, ProjectFrame::updateSpriteXY)
    EVT_CHECKBOX(ID_CHECKBOX1, ProjectFrame::updateMotion)
    EVT_TOGGLEBUTTON(ID_ADD_BONE_BUTTON, ProjectFrame::addBoneButtonToggle)
    EVT_TOGGLEBUTTON(ID_CUSTOM3, ProjectFrame::playButtonToggle)
    EVT_TOGGLEBUTTON(ID_CUSTOM2, ProjectFrame::pauseButtonToggle)
    EVT_TOGGLEBUTTON(ID_CUSTOM4, ProjectFrame::stopButtonToggle)
END_EVENT_TABLE()

ProjectFrame::ProjectFrame(wxWindow* parent,wxWindowID id)
{
    newFileDialog   = NULL;
    boneManager     = NULL;
    config          = new Config();
    addBoneMode     = NULL;
    isPlaying       = NULL;

    wxBitmap addButtonIcon(_("./data/add_bone_icon.png"), wxBITMAP_TYPE_PNG);
    wxBitmap playButtonIcon(_("./data/play.png"), wxBITMAP_TYPE_PNG);
    wxBitmap stopButtonIcon(_("./data/stop.png"), wxBITMAP_TYPE_PNG);
    wxBitmap pauseButtonIcon(_("./data/pause.png"), wxBITMAP_TYPE_PNG);

    //(*Initialize(ProjectFrame)

    Create(parent, wxID_ANY, _("bonyfy"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));
    FlexGridSizer1 = new wxFlexGridSizer(1, 3, 0, 0);
    FlexGridSizer1->AddGrowableCol(0);
    FlexGridSizer1->AddGrowableRow(0);
    BoxSizer4 = new wxBoxSizer(wxVERTICAL);
    addBoneButton = new wxCustomButton(this,ID_ADD_BONE_BUTTON, addButtonIcon);
    BoxSizer4->Add(addBoneButton, 1, wxTOP|wxBOTTOM|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer1->Add(BoxSizer4, 1, wxALL|wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL, 5);
    FlexGridSizer2 = new wxFlexGridSizer(2, 1, 0, 0);
    FlexGridSizer2->AddGrowableCol(0);
    FlexGridSizer2->AddGrowableRow(0);
    GridSizer1 = new wxGridSizer(1, 1, 0, 0);
    FlexGridSizer2->Add(GridSizer1, 1, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    timeLine = new TimeLine(this, 0, this, 0, ID_TIMELINE,wxDefaultPosition,0,_T("ID_TIMELINE"));
    FlexGridSizer2->Add(timeLine, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer1->Add(FlexGridSizer2, 1, wxTOP|wxBOTTOM|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer3 = new wxFlexGridSizer(2, 1, 0, 0);
    FlexGridSizer3->AddGrowableCol(0);
    FlexGridSizer3->AddGrowableRow(0);
    BoxSizer2 = new wxBoxSizer(wxVERTICAL);
    boneTreeCtrl = new BoneTreeCtrl(this,ID_BONE_TREE,wxDefaultPosition,wxDefaultSize,wxTR_DEFAULT_STYLE,wxDefaultValidator,_T("ID_BONE_TREE"));
    BoxSizer2->Add(boneTreeCtrl, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Tabs = new wxNotebook(this, ID_TABS, wxDefaultPosition, wxSize(-1,-1), 0, _T("ID_TABS"));
    Tabs->SetMaxSize(wxSize(-1,50));
    Panel1 = new wxPanel(Tabs, ID_PANEL1, wxDefaultPosition, wxDefaultSize, 0, _T("ID_PANEL1"));
    BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    FlexGridSizer4 = new wxFlexGridSizer(5, 2, 0, 0);
    FlexGridSizer4->AddGrowableCol(0);
    FlexGridSizer4->AddGrowableRow(0);
    StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("Name:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    FlexGridSizer4->Add(StaticText1, 1, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    TextCtrl2 = new wxTextCtrl(Panel1, ID_TEXTCTRL2, _("Text"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
    FlexGridSizer4->Add(TextCtrl2, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText6 = new wxStaticText(Panel1, ID_STATICTEXT6, _("Min loops:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT6"));
    FlexGridSizer4->Add(StaticText6, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    minAngleSpin = new wxSpinCtrlDbl(Panel1,ID_MIN_SPIN);
    FlexGridSizer4->Add(minAngleSpin, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText7 = new wxStaticText(Panel1, ID_STATICTEXT7, _("Max loops:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT7"));
    FlexGridSizer4->Add(StaticText7, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    maxAngleSpin = new wxSpinCtrlDbl(Panel1,ID_MAX_SPIN);
    FlexGridSizer4->Add(maxAngleSpin, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText8 = new wxStaticText(Panel1, ID_STATICTEXT8, _("Loops:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT8"));
    FlexGridSizer4->Add(StaticText8, 1, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    LoopsSpinCrtl = new wxSpinCtrlDbl(Panel1,ID_CUSTOM1);
    FlexGridSizer4->Add(LoopsSpinCrtl, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText9 = new wxStaticText(Panel1, ID_STATICTEXT9, _("Motion:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT9"));
    FlexGridSizer4->Add(StaticText9, 1, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    CheckBox1 = new wxCheckBox(Panel1, ID_CHECKBOX1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
    CheckBox1->SetValue(true);
    FlexGridSizer4->Add(CheckBox1, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3->Add(FlexGridSizer4, 1, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 5);
    Panel1->SetSizer(BoxSizer3);
    BoxSizer3->Fit(Panel1);
    BoxSizer3->SetSizeHints(Panel1);
    Panel2 = new wxPanel(Tabs, ID_PANEL2, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL2"));
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    FlexGridSizer6 = new wxFlexGridSizer(5, 2, 0, 0);
    FlexGridSizer6->AddGrowableCol(0);
    FlexGridSizer6->AddGrowableRow(0);
    StaticText2 = new wxStaticText(Panel2, ID_STATICTEXT2, _("Source:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
    FlexGridSizer6->Add(StaticText2, 1, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    TextCtrl1 = new wxTextCtrl(Panel2, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    FlexGridSizer6->Add(TextCtrl1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer6->Add(-1,-1,1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Button1 = new wxButton(Panel2, ID_BUTTON1, _("Browse"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    FlexGridSizer6->Add(Button1, 1, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    StaticText3 = new wxStaticText(Panel2, ID_STATICTEXT3, _("Pos x:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT3"));
    FlexGridSizer6->Add(StaticText3, 1, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    TextCtrl3 = new wxTextCtrl(Panel2, ID_TEXTCTRL3, _("0"), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_TEXTCTRL3"));
    FlexGridSizer6->Add(TextCtrl3, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText4 = new wxStaticText(Panel2, ID_STATICTEXT4, _("Pos y:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT4"));
    FlexGridSizer6->Add(StaticText4, 1, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    TextCtrl4 = new wxTextCtrl(Panel2, ID_TEXTCTRL4, _("0"), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_TEXTCTRL4"));
    FlexGridSizer6->Add(TextCtrl4, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(FlexGridSizer6, 1, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 5);
    Panel2->SetSizer(BoxSizer1);
    BoxSizer1->Fit(Panel2);
    BoxSizer1->SetSizeHints(Panel2);
    Tabs->AddPage(Panel1, _("PARAMS"), false);
    Tabs->AddPage(Panel2, _("SPRITE"), false);
    BoxSizer2->Add(Tabs, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer3->Add(BoxSizer2, 1, wxBOTTOM|wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL, 5);
    BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    PlayButton = new wxCustomButton(this,ID_CUSTOM3, playButtonIcon);
    BoxSizer5->Add(PlayButton, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    PauseButton = new wxCustomButton(this,ID_CUSTOM2, pauseButtonIcon);
    BoxSizer5->Add(PauseButton, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StopButton = new wxCustomButton(this,ID_CUSTOM4, stopButtonIcon);
    BoxSizer5->Add(StopButton, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer3->Add(BoxSizer5, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer1->Add(FlexGridSizer3, 1, wxTOP|wxBOTTOM|wxRIGHT|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    SetSizer(FlexGridSizer1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem3 = new wxMenuItem(Menu1, idMenuNewFile, _("&New ...\tCtrl+N"), _("Start a new project"), wxITEM_NORMAL);
    Menu1->Append(MenuItem3);
    MenuItem4 = new wxMenuItem(Menu1, idMenuSaveAs, _("&Save as...\tCtrl+S"), _("Save current project as a new file"), wxITEM_NORMAL);
    Menu1->Append(MenuItem4);
    MenuItem6 = new wxMenuItem(Menu1, idMenuOpen, _("&Open\tCtrl+O"), _("Open existing project file"), wxITEM_NORMAL);
    Menu1->Append(MenuItem6);
    MenuItem5 = new wxMenuItem(Menu1, idMenuImport, _("&Import Skeleton\tCrtl+I"), _("Import skeleton from existing file"), wxITEM_NORMAL);
    Menu1->Append(MenuItem5);
    Menu1->AppendSeparator();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    SpritePicker = new wxFileDialog(this, _("Select image file"), wxEmptyString, wxEmptyString, _("*.png"), wxFD_DEFAULT_STYLE, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));
    FileDialogOpen = new wxFileDialog(this, _("Select bonyfy animation file"), _("./samples/"), wxEmptyString, _("bonyfy animation file (*.bnf)|*.bnf"), wxFD_DEFAULT_STYLE|wxFD_OPEN|wxFD_FILE_MUST_EXIST, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));
    FileDialogSave = new wxFileDialog(this, _("Enter name..."), _("./samples/"), wxEmptyString, _("bonyfy animation file (*.bnf)|*.bnf"), wxFD_DEFAULT_STYLE|wxFD_SAVE, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));
    FlexGridSizer1->Fit(this);
    FlexGridSizer1->SetSizeHints(this);
    Center();

    Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&ProjectFrame::OnTextCtrl1Text);
    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&ProjectFrame::OnBrowseClick);
    Connect(idMenuNewFile,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&ProjectFrame::OnNewFile);
    Connect(idMenuSaveAs,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&ProjectFrame::OnMenuSaveAs);
    Connect(idMenuOpen,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&ProjectFrame::OnMenuOpen);
    Connect(idMenuImport,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&ProjectFrame::OnMenuItemImport);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&ProjectFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&ProjectFrame::OnAbout);
    //*)

    boneTreeCtrl->Enable(0);
    Tabs->Enable(0);
    addBoneButton->Enable( FALSE );
    TextCtrl1->SetEditable(0);
    LoopsSpinCrtl->SetRange(-500, 500);
    minAngleSpin->SetRange(-500, 500);
    maxAngleSpin->SetRange(-500, 500);

    LoopsSpinCrtl->SetIncrement(0.001);
    minAngleSpin->SetIncrement(0.001);
    maxAngleSpin->SetIncrement(0.001);

    MenuBar1->Enable(idMenuImport, FALSE);
    MenuBar1->Enable(idMenuSaveAs, FALSE);

    PlayButton->Enable( FALSE );
    PauseButton->Enable( FALSE );
    StopButton->Enable( FALSE );
}

ProjectFrame::~ProjectFrame()
{
    //(*Destroy(ProjectFrame)
    //*)
}

void ProjectFrame::OnQuit(wxCommandEvent& event)
{
    Destroy();
}

void ProjectFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = _T("bonyfy");
    wxMessageBox(msg, _("Welcome to..."));
}

void ProjectFrame::OnNewFile(wxCommandEvent& event)
{
    if( boneManager )
    {
        int answer = wxMessageBox( _("Close the current project and create a new one?"), _("Close..."), wxYES_NO, this);
        if (answer == wxNO)
            return;
    }

    if ( !newFileDialog )
    {
        newFileDialog = new NewFileDialog(this, config);

        if ( newFileDialog->ShowModal() )
        {
            newBoneManager();

//            if( (dsp.GetWidth() < crr.GetWidth() || dsp.GetHeight() < dsp.GetWidth()) )
//            {
//                boneManager->SetWindowStyle( wxHSCROLL | wxVSCROLL );
//                boneManager->ToggleWindowStyle( wxHSCROLL | wxVSCROLL );
//            }
        }

        newFileDialog->Destroy();
        newFileDialog = NULL;
    }
}

void ProjectFrame::CanvasUpdate(wxUpdateUIEvent& event)
{
    boneManager->draw();
    timeLine->draw();
}

void ProjectFrame::updateAngleMin(wxSpinEvent& event)
{
    boneManager->getSelectedBone()->setAngleMin(minAngleSpin->GetValue());
}

void ProjectFrame::updateAngleMax(wxSpinEvent& event)
{
    boneManager->getSelectedBone()->setAngleMax(maxAngleSpin->GetValue());
}

void ProjectFrame::updateLoops(wxSpinEvent& event)
{
    double loopsRelative = LoopsSpinCrtl->GetValue();

    double max = maxAngleSpin->GetValue();
    double min = minAngleSpin->GetValue();

    if (max != 0 && min != 0 )
    {
        if (loopsRelative < min)
            loopsRelative = min;
        if (loopsRelative > max)
            loopsRelative = max;
        setBoneLoops( loopsRelative);
    }

    boneManager->getSelectedBone()->setLoopsRelative(loopsRelative);

    double angle = -boneManager->getSelectedBone()->getLoopsOrginal()*2*M_PI - loopsRelative *2*M_PI;

    boneManager->getSelectedBone()->setAngle(angle);
    boneManager->Refresh();
}

void ProjectFrame::updateBoneName(wxCommandEvent& event)
{
    boneManager->getSelectedBone()->setName(TextCtrl2->GetValue());
    boneManager->rebildSkeletonTree();
}

void ProjectFrame::updateSpriteXY(wxCommandEvent& event)
{
    long x, y;
    if (  ! TextCtrl3->GetValue().ToLong(&x)
        ||! TextCtrl4->GetValue().ToLong(&y) )
    {  std::cerr << "Fail in recreating skeleton from file, wrong data - error in parsing ToLong"; return; }

    boneManager->getSelectedBone()->setSpritePos(wxPoint (x, y));
    boneManager->Refresh();
}

void ProjectFrame::updateMotion(wxCommandEvent& event)
{
    boneManager->getSelectedBone()->setMotion(CheckBox1->IsChecked());
}

void ProjectFrame::setBoneInfo(double loopsRelative, double angleMin, double angleMax, wxString name, bool motion, wxString spriteFileName, wxPoint spritePos)
{
    setBoneLoops(loopsRelative);
    this->minAngleSpin->SetValue(angleMin);
    this->maxAngleSpin->SetValue(angleMax);

    this->TextCtrl2->SetValue(name);
    this->CheckBox1->SetValue(motion);

    this->TextCtrl1->SetValue(spriteFileName);
    this->TextCtrl3->SetValue( wxString::Format(wxT("%i"),spritePos.x) );
    this->TextCtrl4->SetValue( wxString::Format(wxT("%i"),spritePos.y) );
}

void ProjectFrame::setBoneLoops(double loopsRelative)
{
    if ( fabs(loopsRelative) < 0.0001 )
        loopsRelative = .0;
    this->LoopsSpinCrtl->SetValue(loopsRelative);
}

void ProjectFrame::OnEraseBackground(wxEraseEvent& event)
{

}

void ProjectFrame::OnResize(wxSizeEvent& event)
{
    wxColour backgroundColour = GetBackgroundColour();
    if (!backgroundColour.Ok())
        backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);

    SetBackgroundColour(backgroundColour);

    Layout();
    //Update();
    ClearBackground();
    //Refresh();
}

void ProjectFrame::OnTabsPageChanged(wxNotebookEvent& event)
{
}

void ProjectFrame::OnTabsPageChanged1(wxNotebookEvent& event)
{
}

void ProjectFrame::OnTextCtrl1Text(wxCommandEvent& event)
{
    //if(TextCtrl1->LoadFile( TextCtrl1->GetValue() ) );
}

void ProjectFrame::OnBrowseClick(wxCommandEvent& event)
{

    if ( SpritePicker->ShowModal() == wxID_OK )
    {
        wxString filename = SpritePicker->GetFilename();

        boneManager->getSelectedBone()->setSprite( wxImage(filename) );
        boneManager->getSelectedBone()->setSpriteFileName(filename);
        TextCtrl1->SetValue( filename );
        Refresh();
    }
}

void ProjectFrame::OnMenuSaveAs(wxCommandEvent& event)
{
    wxString filename;

    if ( FileDialogSave->ShowModal() == wxID_OK )
    {
        filename=FileDialogSave->GetPath();
        boneManager->saveSkeletonToFile(filename, 1);
    }
}

void ProjectFrame::OnMenuOpen(wxCommandEvent& event)
{
    if( boneManager )
    {
        int answer = wxMessageBox( _("Close the current project and create a new one?"), _("Close..."), wxYES_NO, this);
        if (answer == wxNO)
            return;
    }

    wxString filename;

    if ( FileDialogOpen->ShowModal() == wxID_OK )
    {
        filename=FileDialogOpen->GetPath();
        newBoneManager();
        boneManager->loadSkeletonFromFile(filename, 1);
    }
}

void ProjectFrame::newBoneManager()
{
    MenuBar1->Enable(idMenuImport, TRUE);
    MenuBar1->Enable(idMenuSaveAs, TRUE);

    PlayButton->Enable( TRUE );
    PauseButton->Enable( TRUE );
    StopButton->Enable( TRUE );

    if( boneManager )
    {
        delete boneManager;
        timeLine->reset();
    }

    boneManager = new BoneManager( this, config->getCanvasSize(), config, boneTreeCtrl, NULL, ID_CANVAS);
    GridSizer1->Add(boneManager, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);

    wxSize dsp = wxGetDisplaySize();
    wxSize crr;

    this->SetMinSize( crr = GetBestSize() );
    this->Layout();
    this->Fit();

    boneManager->Refresh();
    this->Refresh();

    boneTreeCtrl->Enable(1);
    Tabs->Enable(1);

    timeLine->setAnimationManager( boneManager->getAnimationManager() );
    timeLine->setConfig( config );
}

void ProjectFrame::OnMenuItemImport(wxCommandEvent& event)
{
    if( boneManager->getRootBone() )
    {
        wxMessageBox( _("Sorry! Can't manage more than one skeleton at one time.\nMaybe some day..."), _("Sorry!") );
        return;
    }

    wxString filename;

    if ( FileDialogOpen->ShowModal() == wxID_OK )
    {
        filename=FileDialogOpen->GetPath();
        boneManager->loadSkeletonFromFile(filename, 0);
    }
}

void ProjectFrame::addBoneButtonToggle(wxCommandEvent& event)
{
    addBoneMode = !addBoneMode;
}

void ProjectFrame::playButtonToggle(wxCommandEvent& event)
{
    if( PlayButton->GetValue() )
    {
        boneManager->getAnimationManager()->start();
        PauseButton->SetValue( 0 );
        StopButton->SetValue( 0 );
    }
    else
        boneManager->getAnimationManager()->pause();
}

void ProjectFrame::pauseButtonToggle(wxCommandEvent& event)
{
    if( PauseButton->GetValue() )
    {
        boneManager->getAnimationManager()->pause();
        PlayButton->SetValue( 0 );
        StopButton->SetValue( 0 );
    }
    else
        boneManager->getAnimationManager()->start();

}

void ProjectFrame::stopButtonToggle(wxCommandEvent& event)
{
    if( StopButton->GetValue() )
    {
        boneManager->getAnimationManager()->stop();
        PlayButton->SetValue( 0 );
        PauseButton->SetValue( 0 );
    }
}

int ProjectFrame::modeType()
{
    return addBoneMode;
}

void ProjectFrame::enableAddBoneMode( bool value )
{
    if( value )
        addBoneButton->Enable( TRUE );
    else
    {
        //boneManager->setBonesOrginalAngles();
        addBoneButton->SetValue( FALSE );
        addBoneButton->Enable( FALSE );
        addBoneMode = FALSE;
    }
    addBoneButton->Refresh();
}

TimeLine* ProjectFrame::getTimeLine() const
{
    return timeLine;
}
